# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
require "app_config"

class ApplicationController < ActionController::Base
  layout "index"

  # maps user type to action performed to authorize specific user
  USER_TO_AUTHORIZATION = {
    UserType::STUDENT => :student_authorized?,
    UserType::TEACHER => :teacher_authorized?,
    UserType::AUTHOR => :author_authorized?,
    UserType::ADMINISTRATOR => :admin_authorized?
  }

  private
  def set_editing_layout
    @editing_layout = true
  end 
    
  def get_flash_info
    flash[:info]
  end

  def set_flash_info(fi_)
    flash[:info] = fi_
  end
  
  def clear_flash_info
    set_flash_info(nil) 
  end

  def get_session_user
    session[:user]
  end
  
  def set_session_user(user_)
    session[:user] = user_ && user_.refresh
  end

  def clear_session_user
    set_session_user(nil) 
  end

  def get_requested_uri
    session[:requested_url]
  end

  def set_requested_uri
    session[:requested_url] = url_for(params)
  end
  
  def clear_requested_uri
    session[:requested_url] = nil 
  end

  # Redirects to login page if there is no user in session 
  # or if user is no longer in the database    
  def authorize
    su = get_session_user
    authorized = !su.nil? && User.find_by_login(su.login)
    
    unless authorized
      on_not_authorized("Please login.")
    end
    
    authorized
  end

  # Does the same as "authorize" plus authorization of specific user  
  def authorize_user(type_)
    return false unless authorize

    authorized = send USER_TO_AUTHORIZATION[type_]

    unless authorized
      on_not_authorized("Please login as " + type_[:name])
    end

    authorized
  end

  # authorizes student
  def authorize_student
    authorize_user UserType::STUDENT
  end

  # authorizes teacher
  def authorize_teacher
    authorize_user UserType::TEACHER
  end

  # authorizes author
  def authorize_author
    authorize_user UserType::AUTHOR
  end

  # authorizes admin
  def authorize_admin
    authorize_user UserType::ADMINISTRATOR
  end

  # returns true if user in session can be authorized as a student
  def student_authorized?
    get_session_user.has_student_privs?
  end

  # returns true if user in session can be authorized as a teacher
  def teacher_authorized?
    get_session_user.has_teacher_privs?
  end

  # returns true if user in session can be authorized as an author
  def author_authorized?
    get_session_user.has_author_privs?
  end

  # returns true if user in session can be authorized as an admin
  def admin_authorized?
    get_session_user.has_admin_privs?
  end

  # invoked if user can not be authorized
  # caches requested uri and redirects to login page
  def on_not_authorized(msg_ = nil)
    set_requested_uri
    set_flash_info(FlashInfo.get_error("You have not enough privileges to view requested page. " + (msg_ || "")))
    redirect_to(:controller => "login")
  end

  def get_map(div_id)
    map = Cartographer::Gmap.new(div_id)
    map.center = [MAP[:center][:latitude], MAP[:center][:longitude]]
    map.zoom = MAP[:zoom]
    map.type = :hybrid
    map.controls = []
    map.markers = []
    map.mouse_wheel_zoom = true
    map.double_click_zoom = true
    map.continuous_zoom = true
    map
  end

  def float_is_between (a, b, c)
    max = b > c ? b : c
    min = b > c ? c : b
    a >= min  && a <= max
  end

  # Shows invalid action page if condition_ is true
  # Returns value of condition_
  # Use this if given action expect any parameters or should meet 
  # additional conditions to run properly
  # 
  # def some_action
  #   return false if check_params(params[:user].nil?)
  # end
  def check_params(condition_)
    render :partial => "/templates/bad_params" if condition_
    condition_
  end

  def check_record(condition_)
    render :partial => "/templates/no_record" if condition_
    condition_
  end
  
  def datetime_str(y,m,d,h,mn,s)
    y.to_s.rjust(4,"0") + "-" + m.to_s.rjust(2,"0") + "-" + d.to_s.rjust(2,"0") + " "  + h.to_s.rjust(2,"0") + ":" + mn.to_s.rjust(2,"0")+ ":" + s.to_s.rjust(2,"0")
  end

  def format_bytes(bytes_)
  	bytes_ = bytes_.to_f
  
  	kb = bytes_/1024
  	kb_tot = kb.floor
  	bytes_rem = bytes_ - 1024*kb_tot
  
  	return format("%dB", bytes_) if kb_tot < 1
  
  	mb = kb/1024
  	mb_tot = mb.floor
  	kb_rem = kb - 1024*mb_tot
  
  	return format("%3.1fKB", kb) if mb_tot < 1
  
  	gb = mb/1024
  	gb_tot = gb.floor
  	mb_rem = mb - 1024*gb_tot
  
  	return format("%3.1fMB", mb) if gb_tot < 1
  	return format("%.1fGB", gb)
  end
end